package com.cjx.leetcode.backtracking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author cjx
 * @description E90.子集 II
 * @createDate 2024/4/5 9:59
 **/
public class E90 {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        boolean [] used = new boolean[nums.length];
        backtracking(nums,0,used);
        return result;
    }

    private void backtracking(int[] nums, int startIndex , boolean[] used) {
        result.add(new ArrayList<>(path));
        for (int i = startIndex; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backtracking(nums, i + 1,used);
            path.remove(path.size() - 1);
            used[i] =false;
        }
    }
}
